<?php


namespace app\admin\controller;


use app\admin\model\AdminUser;
use app\admin\model\AdminRole;
use think\Controller;
use think\Request;
use think\Validate;


class User extends Controller {

    private $adminModel;
    private $roleModel;

    protected $middleware = [
        "CheckPermission",
        "Log",
    ];

    public function initialize()
    {
        parent::initialize();
        $this->adminModel = new AdminUser();
        $this->roleModel = new AdminRole();
    }


    public function index(Request $request){

        $params = $request->get();
        $username = $params['username']??'';
        $where[] = ['is_del','=',0];

        if($username!='') $where[] = ['username','like','%' . trim($username) . '%'];

        $list = $this->adminModel->getAdminList($where,15,$params);

        return $this->view->fetch('index',['list'=>$list,'total'=>$list->total()]);
    }

    // 添加管理员
    public function add(Request $request){

        if($request->isPost()){

            $post = $request->post();

            $rule = [
                'username|账号'=>'require',
                'role_id|角色'=>'require',
                'realname|姓名'=>'require',
                'password|密码'=>'require',
                'email|邮箱'=>'require|email',
                'mobile|手机'=>'require'
            ];

            $message = [
                'require'=>'不能为空',
                'role_id.require'=>'请选择角色',
                'email'=>'邮箱格式不正确'
            ];

            //验证
            $validator = Validate::make($rule, $message);
            if(!$validator->check($post)){
                $this->error('提示：'.$validator->getError());
            }

            $post['salt'] = $salt = getNonceStr(6);
            $post['password'] = md5(trim($post['password']).$salt);

            $id = $this->adminModel->insertOne($post);

            if ($id) {

                $this->success('添加管理员成功',url('admin/user/index'));
            } else {

                $this->error('添加管理员失败');
            }

        } else {
            $roles = $this->roleModel->getAll();

            return $this->view->fetch('add',['roles'=>$roles]);
        }
    }


    // 编辑管理员
    public function edit(Request $request){

        if($request->isPost()){

            $post = $request->post();

            $rule = [
                'username|账号'=>'require',
                'admin_id'=>'require',
                'role_id|角色'=>'require',
                'realname|姓名'=>'require',
                'email|邮箱'=>'require|email',
                'mobile|手机'=>'require'
            ];

            $message = [
                'require'=>'不能为空',
                'role_id.require'=>'请选择角色',
                'email'=>'邮箱格式不正确'
            ];

            //验证
            $validator = Validate::make($rule, $message);
            if(!$validator->check($post)){
                $this->error('提示：'.$validator->getError());
            }

            $user = $this->adminModel->getOne($post['admin_id']);

            if($post['password']!=''){
                $post['password'] = md5(trim($post['password']).$user['salt']);
            } else {
                unset($post['password']);
            }

            unset($post['admin_id']);

            $rt = $this->adminModel->updateOne($user['admin_id'], $post);

            if ($rt) {

                $this->success('编辑管理员成功',url('admin/user/index'));
            } else {

                $this->error('编辑管理员失败');
            }

        } else {
            $admin_id = $request->get('admin_id',0);
            if($admin_id==0) $this->error('参数错误admin_id不能为空');

            $roles = $this->roleModel->getAll();
            $arr = $this->adminModel->getOne($admin_id);

            return $this->view->fetch('edit',['roles'=>$roles,'info'=>$arr]);
        }
    }


    // 删除管理员
    public function del(Request $request){
        $params = $request->get();

        $admin_id = $params['admin_id']??0;
        if ($admin_id>0) {

            $rt = $this->adminModel->delOne($admin_id);
            if ($rt) {

                $res['err'] = 0;
                $res['msg'] = '删除成功';
                return response($res,200,[],'json');

            } else {
                $res['err'] = 1002;
                $res['msg'] = '删除失败';
                return response($res,200,[],'json');
            }

        } else {

            $res['err'] = 1001;
            $res['msg'] = '参数错误admin_id不能为空';
            return response($res,200,[],'json');
        }

    }


    // 禁用/开启管理员
    public function setStatus(Request $request){

        $params = $request->get();
        $admin_id = $params['admin_id']??0;
        $status = $params['status']??0;

        if ($admin_id>0) {

            $data['status'] = $status;
            $rt = $this->adminModel->updateOne($admin_id,$data);
            if ($rt) {

                $res['err'] = 0;
                $res['msg'] = '设置成功';
                return response($res,200,[],'json');

            } else {
                $res['err'] = 1002;
                $res['msg'] = '设置失败';
                return response($res,200,[],'json');
            }

        } else {

            $res['err'] = 1001;
            $res['msg'] = '参数错误admin_id不能为空';
            return response($res,200,[],'json');
        }

    }



}